package com.mytime.entity;

import org.json.JSONException;
import org.json.JSONObject;

import com.mytime.util.Date;
import com.mytime.util.Time;

public class Event {
	
	private String id;
	private String name;
	private double cost;
	private Time duration;
	private Time startTime;
	private Time endTime;
	private Date startDate;
	private Date endDate;
	private Local local;
	
	public Event() {
		this.startTime = new Time();
		this.endTime = new Time();
	}
	
	public Event(JSONObject eventObj) throws JSONException {		
		this.startTime = new Time();
		this.endTime = new Time();
		this.local = new Local();
		
		if (eventObj.has("id"))
			this.id = eventObj.getString("id");
		
		if (eventObj.has("name"))
			this.name = eventObj.getString("name");
				
		if (eventObj.has("startTime"))
			this.startTime.changeTime(eventObj.getString("startTime"));
		
		if (eventObj.has("endTime"))
			this.endTime.changeTime(eventObj.getString("endTime"));

		if (eventObj.has("local"))
			this.local = new Local(eventObj.getJSONObject("local"));

		this.duration = Time.subtract(this.startTime, this.endTime);
	}
	
	//***************//
	//Public Methods//
	//*************//

	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
		
	public double getCost() {
		return cost;
	}
	
	public void setCost(double cost) {
		this.cost = cost;
	}

	public Time getDuration() {
		return duration;
	}

	public void setDuration(Time duration) {
		this.duration = duration;
	}

	public Time getStartTime() {
		return startTime;
	}
	
	public void setStartTime(String startTime) {
		this.startTime.changeTime(startTime);
	}
	
	public Time getEndTime() {
		return endTime;
	}
	
	public void setEndTime(String endTime) {
		this.endTime.changeTime(endTime);
	}
	
	public Date getStartDate() {
		return startDate;
	}
	
	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}
	
	public Date getEndDate() {
		return endDate;
	}
	
	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Local getLocal() {
		return local;
	}

	public void setLocal(Local local) {
		this.local = local;
	}

	public JSONObject toJSON() throws JSONException {
		JSONObject event = new JSONObject();
		
		try {
			event.put("id", id);
			event.put("name", name);
			event.put("startTime", startTime.getTime());
			event.put("endTime", endTime.getTime());
			event.put("duration", duration);
			
		} catch (JSONException e) {
			event.put("error", "Problem transforming an event into a JSONObject");
			e.printStackTrace();
		}
		
		return event;
	}

}

